<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"
      layout:decorator="base/layout.html">


<!-- start: Content -->
<div layout:fragment="content" class="main ">

    <div class="row">
        <div class="col-lg-12">
            <ol class="breadcrumb">
                <li><i class="fa fa-home"></i><a th:href="@{../infoManage/indexTotal}">首页</a></li>
                <li><i class="fa fa-list-alt"></i><a th:href="@{../infoManage/policyDocument}">政策文件管理</a></li>
                <li><i class="fa fa-indent"></i>修改政策文件</li>
            </ol>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h2><i class="fa fa-indent red"></i><strong>修改政策文件</strong></h2>
                </div>
                <div class="panel-body">
                    <form id="modularForm" method="post" enctype="multipart/form-data" class="form-horizontal ">
                        <input id="infoManageId" type="hidden" name="id" th:value="${infoManage.id}"/>

                        <div class="form-group">
                            <label class="col-md-3 control-label" for="text-input">标题</label>
                            <div class="col-md-6">
                                <input type="text" id="title" name="title" class="form-control" placeholder="标题"
                                       th:value="${infoManage.title}" required>
                                <span class="help-block">请修改标题(必填)</span>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-md-3 control-label" for="text-input">内容</label>
                            <div class="col-md-9">
                                   <textarea class="layui-textarea" id="introduction" style="display: none" th:text="${infoManage.introduction}"required>


					                </textarea>
                            </div>
                        </div>

                        <div id="fileDiv" th:if="${#lists.isEmpty(infoManage.fileurlList)}"
                             class="form-group">
                            <label class="col-md-3 control-label">政策文件</label>
                            <div class="col-md-2">
                                <input type="file" id="file-multiple-input" name="fileUpload" multiple>
                            </div>

                            <span class="col-md-1">
                                    <button type="button" class=" btn btn-sm btn-info" onclick="addFileUrl()" > 添加</button>
                                </span>
                        </div>

                        <div th:unless="${#lists.isEmpty(infoManage.fileurlList)}"
                             th:each="fileurl,state  : ${infoManage.fileurlList}"
                             class="form-group" th:id="${'fileDiv'+state.index}">
                            <label class="col-md-3 control-label">合作协议</label>
                            <div th:id="${'fileDivFile'+state.index}" class="col-md-3">

                                <a id="fileurlName" th:href="@{downloadFile(fileName=${fileurl})}" th:text="${fileurl}"></a>
                            </div>
                            <span th:id="${'fileDivSpan'+state.index}" class="col-md-1">
                                    <button type="button" class=" btn btn-sm btn-info" th:onclick="${'deleteFileUrl('+state.index+')'}" > 删除</button>
                                </span>

                            <span id="fileDivAddSpan" th:if="${state.index==0}" class="col-md-1">
                                    <button type="button" class=" btn btn-sm btn-info" onclick="addFileUrl()" > 添加</button>
                                </span>
                        </div>
                        <br id="a">
                </div>
                <div class="panel-footer">
                    <button id="submitBtn" type="submit" class="btn btn-sm btn-success"><i
                            class="fa fa-dot-circle-o"></i> 保存
                    </button>
                    <button type="button" class="btn btn-sm btn-success" onclick="history.back()"><i
                            class="fa fa-dot-circle-o"></i> 返回
                    </button>
                </div>
                </form>
            </div>
        </div>
    </div><!--/.row-->
    <script>
        var layedit, index;
        $(document).ready(function () {

            var pro = window.location.protocol;
            var host = window.location.host;
            var domain = pro + "//" + host;

            layui.use(['layedit', 'upload'], function () {
                layedit = layui.layedit;
                layedit.set({
                    uploadImage: {
                        url: 'uploadLayuiFile?domain=' + domain,
                        type: 'post'
                    }
                });
                index = layedit.build('introduction');
            });

        })



        //添加文件按钮
        function  addFileUrl() {
           var fileUrlList="[[${infoManage.fileurlList}]]";
            if(fileUrlList==undefined||fileUrlList==null||fileUrlList==""){
                var addItem = $('#fileDiv').clone(true);
                $(addItem).find("button").html("删除").attr("onclick","javascript:$(this.parentNode.parentNode).remove();");
                $(addItem).find("input").val("");
                $('#a').before(addItem);
            }
            else{
                var addItem = $('#fileDiv0').clone(true);
                //变成空文件
                $(addItem).find("div").html("<input type='file' id='file-multiple-input' name='fileUpload' multiple>");
                //删除添加按钮层
                $(addItem).find("#fileDivAddSpan").html("")
                //添加删除按钮层
                $(addItem).find("#fileDivSpan0").html("<button type='button' class='btn btn-sm btn-info' onclick='javascript:$(this.parentNode.parentNode).remove();' > 删除</button>");
                $('#a').before(addItem);
            }


        }
        //删除合作协议按钮
        function deleteFileUrl(deleteNum){

            /**
             * 物理删除  数据库中，合作协议文件名。修改数据库字段
             * 添加确认删除按钮提示框
             *
             */

            layer.confirm('确认删除？', {
                btn: ['确认','取消'] //按钮
            }, function(index){
                //确认
                //首先关闭弹窗
                layer.close(index);

                //获取infoManage主键id
                var id=$("#infoManageId").val();

                //获取要删除的文件名
                var deleteFileurlName=$("#fileDiv"+deleteNum).find("#fileurlName").html();

                $.ajax({
                    url:'deleteFileurlName',
                    type:'get',
                    dataType:'json',
                    data:{id:id,deleteFileurlName:deleteFileurlName},
                    success:function (data) {
                        //删除成功
                        //第一个合作协议
                        if(deleteNum==0){
                            //将a标签变为File标签
                            $('#fileDivFile0').html("<input type='file' id='file-multiple-input' name='fileUpload' multiple>");
                            //删除删除按钮层
                            $('#fileDivSpan0').html("");
                        }
                        //其他合作协议层
                        else{
                            $('#fileDiv'+deleteNum).remove();
                        }
                    }
                })
            }, function(){
                //取消
            });
        }


        $("#modularForm").validate({

            submitHandler: function (form) {

                var formdata = {};


                var content=layedit.getContent(index);

                if (content.length==0||$.trim(content)==""){
                    layer.msg("请输入内容")
                }else{
                    formdata.introduction = layedit.getContent(index);
                    $("#modularForm").ajaxSubmit(
                        {
                            url: 'policyDocumentUpdate',
                            type: 'post',
                            data :formdata,
                            success: function (data) {

                                layer.msg("添加成功");
                                setTimeout(function () {
                                    window.location = ("policyDocument")
                                }, 1500);
                            }
                            //clearForm: false,//禁止清楚表单
                            //resetForm: false //禁止重置表单
                        });
                }
            }
        })

    </script>
</div>
